package com.aolong.archives.core.sysmanage.service.impl;

import net.sf.json.JSONObject;

import com.aolong.archives.common.resource.I18nBundleUtil;
import com.aolong.archives.common.util.DateUtil;
import com.aolong.archives.core.sysmanage.dao.IUserDao;
import com.aolong.archives.core.sysmanage.entity.UserEntity;
import com.aolong.archives.core.sysmanage.service.IUserService;
import com.rock.platform.entity.ActionParamsEntity;
import com.rock.platform.entity.PageEntity;
import com.rock.platform.exception.BaseException;
import com.rock.platform.service.BaseService;
import com.rock.platform.util.GUID;
import com.rock.platform.util.MD5;

public class UserService extends BaseService implements IUserService {

	public IUserDao dao;
	
	public IUserDao getDao() {
		return dao;
	}

	public void setDao(IUserDao dao) {
		this.dao = dao;
	}

	public PageEntity findAll(ActionParamsEntity actionParams)
	{
		return dao.findAll(actionParams);
	}
	
	public void add(UserEntity entity) {
		
		entity.setUserId(GUID.NEWID());
		entity.setPassword(getInitPassword());
		entity.setStatus(1);
		entity.setRegDate(DateUtil.getNowDate());
		dao.add(entity);
	}

	public void remove(Object[] ids) {
		dao.remove(ids, UserEntity.class);
	}

	public void modify(UserEntity entity) {
		dao.modify(entity);
	}

	public void disabled(Object[] ids) {
		dao.disabled(ids);
	}

	public void activate(Object[] ids) {
		dao.activate(ids);
	}

	public void modifyPassword(JSONObject jo) {
		
		String userId = jo.getString("userId");
		String newPassword = jo.getString("newPassword");
		String oldPassword = jo.getString("oldPassword");
		
		UserEntity entity = dao.findByID(userId, false, UserEntity.class);
		
		if (!MD5.verifyPassword(oldPassword, entity.getPassword()))
		{
			throw new BaseException(I18nBundleUtil.getI18n("old password is error")+"!");
		}
		
		this.dao.modifyPassword(MD5.getMD5ofStr(newPassword), userId);
	}

	public void resetPassword(Object[] ids) {
		this.dao.resetPassword(getInitPassword(), ids);
	}
	
	private String getInitPassword()
	{
		return MD5.getMD5ofStr("123456",2);
	}
}
